
#define APP_INIT_C
#include "app_includes.h"

#define mainCREATOR_TASK_PRIORITY           ( tskIDLE_PRIORITY + 3 )
#define TASK_PRORITY_LED 3

uint8_t app_init_flag = 0;
uint32_t lcd_sleep_time;

void app_init(void)
{
    board_init();
    /* add semaphores, ... */
    xSemaphoreSample = xSemaphoreCreateBinary();
    sys_event =  xEventGroupCreate();
    pd_event =  xEventGroupCreate();
    
    //board_memory_test();
    app_dev_init();
    printf("initial end\n");
    bsp_init_later();
    
    //buzzer_sound(&board.buzzer, 1);
    
    app_dev_para_init();
    led_off(&board.lcd_backlight); //先关闭背光等ui初始化完成
    app_init_flag = 1;
}

void task_led(void *para)
{
    led_breath_set(&board.led[kBoardLed_Work], kLED_breath_mode_0, 2000);
    led_on(&board.led[kBoardLed_Work]);
    
    xEventGroupWaitBits(sys_event,kSysEvent_GUILoad,pdTRUE,pdFALSE,10000); //等待GUI初始化完成
    vTaskDelay(pdMS_TO_TICKS(100));
    led_on(&board.lcd_backlight);
    while(1) {
        
        //taskENTER_CRITICAL();//关中断
        
        app_dev_meter_cal(app_dev.meter.power_v.id, ina226_get_adc(&board.ina226_in, INA226_Ch_Bus));
        app_dev_meter_cal(app_dev.meter.power_i.id, ina226_get_adc(&board.ina226_in, INA226_Ch_Current));
        app_dev_meter_update(app_dev.meter.power_p.id, app_dev.meter.power_v.value_ins*app_dev.meter.power_i.value_ins);
        
        app_dev_meter_cal(app_dev.meter.power_out_v.id, ina226_get_adc(&board.ina226_out, INA226_Ch_Bus));
        app_dev_meter_cal(app_dev.meter.power_out_i.id, ina226_get_adc(&board.ina226_out, INA226_Ch_Current));
        app_dev_meter_update(app_dev.meter.power_out_p.id, app_dev.meter.power_out_v.value_ins*app_dev.meter.power_out_i.value_ins);

        app_dev_meter_update(app_dev.meter.cpu_temp.id, board_get_cpu_temp()*0.64);
        
        //taskEXIT_CRITICAL();//开中断
        device_status_refresh();
        vTaskDelay(pdMS_TO_TICKS(10));
    }
}

void app_timer_task(uint16_t tick)
{
    static uint32_t lcd_sleep_cnt = 0;
    uint16_t i;
    uint8_t change = 0;
    uint8_t press_flag = 0;
    BaseType_t xHigherPriorityTaskWoken = pdFALSE;   
    
    led_breath(&board.led[kBoardLed_Work],tick);
    buzzer_tick(&board.buzzer,tick);
    
    for(i=0;i!=BOARD_KEY_NUM;i++) {
        change |= key_scan(&board.key[i], tick); //扫描按键
        if(board.key[i].event & kKeyEvent_Press)
            press_flag = 1;
    }
    
    if(press_flag) {
        buzzer_sound(&board.buzzer, 1);
    }
    
    if(lcd_sleep_time>0) {
        if(lcd_sleep_cnt >= lcd_sleep_time) {
            if(change) {
                led_on(&board.lcd_backlight);
                lcd_sleep_cnt = 0;
                return;
            }
        }
        else {
            lcd_sleep_cnt += tick;
            if(lcd_sleep_cnt >= lcd_sleep_time) {
                led_off(&board.lcd_backlight);
            }
            if(change)
                lcd_sleep_cnt = 0;
        }
    }
    
    if(change) {
        if(board.key[kBoardKey_PowerEnable].event & kKeyEvent_Press) {
            app_dev.para.run.power_enable ^= 1;
        }
        //lvgl encoder
        if(board.key[kBoardKey_Left].event & kKeyEvent_Press) {
            if (app_dev.para.store.display_dir >= 2) {
                board.enc_diff += 1;
            } else {
                board.enc_diff -= 1;
            }   
        }
        if(board.key[kBoardKey_Right].event & kKeyEvent_Press) {
            if (app_dev.para.store.display_dir >= 2) {
                board.enc_diff -= 1;
            } else {
                board.enc_diff += 1;
            }
        }
        board.enc_enter = board.key[kBoardKey_Enter].state;
    }
}

void app_create_task(void *para)
{
    key_t *key;
    app_init();
    //xTaskCreate(task_sample, "task_sample", 128, NULL, 6, NULL );         //建立采样任务
    xTaskCreate(task_led, "task_led", 128, NULL, 3, NULL );  //建立led任务
    xTaskCreate(task_power, "task_power", 128, NULL, 4, NULL );  //建立power任务
    xTaskCreate(task_pc, "task_comm", 128, NULL, 4, NULL );  //建立通讯任务
    xTaskCreate(task_gui, "lvgl", 1024, NULL, 2, NULL);                     //建立GUI任务
    xTaskCreate(task_dap, "task_dap", 256, NULL, 5, NULL );
   
    key = &board.key[kBoardKey_Enter];
    if(key->bus->read(key->bus_handle)==1) {//如果未检测到波轮按下，启动pd协议
        vTaskDelay(10);
        if(key->bus->read(key->bus_handle)==1)
            xTaskCreate(task_pd, "task_pd", 256, NULL, 5, NULL );
    }

    vTaskDelete(NULL);
}
